import * as appConfig from '../../app-config.js';
var app = getApp()
Page({

  data: {
    telphone:'',
    password:''
  },

  onLoad: function (options) {
    wx.getStorage({
      key:'memInfo',
      success(res){
        console.log('用户已登录')
        wx.switchTab({
          url: '../stock/stock',
        })
      },
      fail(res){
        console.log(res.errMsg+':用户未登录')
      }
    })
  },
  //获取输入框中的账号密码
  bindPhoneInput(e){
    console.log(e.detail.value)
    this.setData({
      telphone:e.detail.value
    })
  },
  bindKeyInput(e){
   this.setData({
    password:e.detail.value
  })
 },

//登陆
loginTap(){
  const that = this
  console.log(this.data.telphone)
  console.log(this.data.password)
  wx.request({
    url: appConfig.apiBase+'wx_app/login',
    data: {
      phone:that.data.telphone,
      pwd:that.data.password
    },
    success(res){
      console.log(res)
      if (res.data.code==1) {
        wx.setStorage({
          key:"memInfo",
          data:{
            agentId:res.data.data.agentId,
            id:res.data.data.id,
            gender:res.data.data.gender,
            username:res.data.data.username,
            name:res.data.data.name,
            role:res.data.data.role,
            isEnable:res.data.data.isEnable
          }
        })
        wx.login({
          success: function(res) {
            if (res.code) {
              wx.request({
                url: appConfig.apiBase+'wx_app/getOpenId',
                data: {
                  code: res.code
                },
                success(res){
                  console.log(res)
                }
              })
            } else {

            }
          }
        });
        wx.switchTab({
          url: '../stock/stock',
        })
      }else{
        app.errorMsg('您输入的的账号密码有误')
      }
    }
  })
},
forgetPassWordTap(event){
  wx.navigateTo({
    url: 'forgetPassWord/forgetPassWord',
  })
},
test(){
  wx.switchTab({
    url: '../stock/stock',
  })
}
})